package com.pactera.asmp.server.entity;

import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.utils.FastJsonUtil;
import java.io.Serializable;

/**
 * @Description 前端请求响应结果, code:编码,message:描述,obj对象
 * @auther: lzq
 * @date: 2019/7/28 14:04
 */
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 7285065610386199394L;
    private int code;
    private String message;
    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResponseResult(){

    }

    public ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponseResult(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();

    }

    public static <T> ResponseResult<T> defaultFail(int code, String msg, T... data) {
        return new ResponseResult<T>(code, msg, data != null && data.length > 0 ? data[0] : null);
    }

    public static <T> ResponseResult<T> defaultFail(ResultCode resultCode, T... data) {
        return new ResponseResult<T>(resultCode.code(), resultCode.message(), data != null && data.length > 0 ? data[0] : null);
    }

    public static <T> ResponseResult<T> defaultSuccess(T data) {
        return new ResponseResult<T>(ResultCode.SUCCESS.code(), ResultCode.SUCCESS.message(), data);
    }

    public static <T> ResponseResult<T> defaultSuccess(String msg, T data) {
        return new ResponseResult<T>(ResultCode.SUCCESS.code(), msg, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return FastJsonUtil.BeanToJson(this);
    }
}
